﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bank
{
    class Bank
    {
        private List<Account> listOfAccounts = new List<Account>();
        
        Account account;
        public void GenerateAccounts()
        {
            account = new Account();
            Console.WriteLine("Please enter the first name of the customer:");
            string firstName = Console.ReadLine();
            Console.WriteLine("Please enter the last name of the customer:");
            string lastName = Console.ReadLine();
            Console.WriteLine("Please enter the ID of the opening account:");
            int id = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the amount of money you want to put:");
            double money = Double.Parse(Console.ReadLine());
            account.GenerateCustomer(firstName, lastName, id, money);
            listOfAccounts.Add(account);
            Console.WriteLine("Account is created.");
        }
        

        public void PrintBankInfo()
        {
            if (listOfAccounts.Contains(account))
            {
                account.PrintFullAccountInfo();
            }
        }

        public void PrintAllAccounts()
        {
            if (listOfAccounts.Any())
            {
                Console.WriteLine("All accounts in the bank:");
                foreach (var account in listOfAccounts)
                {
                    account.PrintFullAccountInfo();
                }
            }
            else
            {
                Console.WriteLine("There is no accounts!");
            }          
        }

        public void DeleteAccount()
        {
            if (listOfAccounts.Any())
            {
                PrintAllAccounts();
                Console.WriteLine("Please choose account number to delete:");
                int choice = Int32.Parse(Console.ReadLine());
                for (int i = 0; i < listOfAccounts.Count; i++)
                {
                    if (choice == listOfAccounts[i].Id)
                    {
                        listOfAccounts.Remove(listOfAccounts[i]);
                    }
                }
                Console.WriteLine("Account is deleted.");
            }
            else
            {
                Console.WriteLine("There is no accounts!");
            }            
        }

        public void AddMoneyToAccount()
        {
            if (listOfAccounts.Any())
            {
                Console.WriteLine("Please choose account number you want to add money:");
                int choice = Int32.Parse(Console.ReadLine());
                if (listOfAccounts.Any(x => x.Id == choice))
                {
                    Console.WriteLine("Please enter amount of money you want to add:");
                    double moneyToAdd = Double.Parse(Console.ReadLine());
                    if (moneyToAdd > 0)
                    {
                        foreach (var account in listOfAccounts)
                        {
                            if (choice == account.Id)
                            {
                                account.AddMoney(moneyToAdd);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Incorrect value. Unable to add \'0\' or less!");
                    }

                }
                else
                {
                    Console.WriteLine("There is no account with such ID.");
                }
            }
            else
            {
                Console.WriteLine("There is no accounts!");
            }
        }

        public void TakeMoneyFromAccount()
        {
            if (listOfAccounts.Any())
            {
                Console.WriteLine("Please choose account number you want to take money from:");
                int choice = Int32.Parse(Console.ReadLine());
                if (listOfAccounts.Any(x => x.Id == choice))
                {
                    Console.WriteLine("Please enter amount of money you want to take:");
                    double moneyToTake = Double.Parse(Console.ReadLine());
                    foreach (var account in listOfAccounts)
                    {
                        if (choice == account.Id)
                        {
                            account.TakeMoney(moneyToTake);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("There is no account with such ID.");
                }
            }
            else
            {
                Console.WriteLine("There is no accounts!");
            }
        }

    }
}
